package com.example.weather.utils;


import com.example.weather.pojo.TokenResult;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TokenUtil {

    public static String getToken() throws Exception{

        //登录获取Token
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n    \"username\":\"18833518978\",\r\n    " +
                "\"password\":\"123456\"\r\n}\r\n");
        Request request = new Request.Builder()
                .url(BaseUrl.baseUrl+"/dev-api/login")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();

        String responseData = response.body().string();
        Gson gson = new Gson();
        TokenResult tokenResult = gson.fromJson(responseData, TokenResult.class);
        return tokenResult.getToken();

    }
}
